package logic

import (
	"context"
	"product666/pkg"
	"product666/userrpc/userrpc"
	"strconv"

	"product666/api/internal/svc"
	"product666/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LonginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLonginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LonginLogic {
	return &LonginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LonginLogic) Longin(req *types.LonginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	longin, err := l.svcCtx.UserRpc.Longin(l.ctx, &userrpc.LonginRequest{
		Username: req.Username,
		Password: req.Password,
		Code:     req.Code,
	})
	if err != nil {
		return &types.Response{
			Message:  err.Error(),
			Code:     "500",
			Database: nil,
		}, nil
	}
	handler, err := pkg.TokenHandler(strconv.FormatInt(longin.Id, 10))
	if err != nil {
		return &types.Response{
			Message:  err.Error(),
			Code:     "500",
			Database: nil,
		}, nil
	}
	return &types.Response{
		Message:  "用户登录成功",
		Code:     "200",
		Database: handler,
	}, nil
}
